<?php
/*
Bitsand - a web-based booking system for LRP events
Copyright (C) 2006 - 2008 The Bitsand Project (http://bitsand.googlecode.com/)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

include ('inc/inc_head_db.php');
include ('inc/inc_forms.php');

//Initialise error message
$sWarn = '';

$db_prefix = DB_PREFIX;

if ($_POST ['btnSubmit'] != '' && CheckReferrer ('ic_form.php')) {
	//No data checks are performed, because there is no requirement to provide IC info in Maelstrom version
	//Character details - check if character exists
	$sql = "SELECT * FROM {$db_prefix}characters WHERE chPlayerID = $PLAYER_ID";
	$result = ba_db_query ($link, $sql);
	//If character does not exist insert a row so that UPDATE query will work
	if (ba_db_num_rows ($result) == 0) {
		$sql = "INSERT INTO {$db_prefix}characters (chPlayerID) VALUES ($PLAYER_ID)";
		if (! ba_db_query ($link, $sql)) {
			$sWarn = "There was a problem updating your IC details";
			LogError ("Error inserting player ID into characters table prior to running UPDATE query.\nPlayer ID: $PLAYER_ID");
		}
	}
	elseif (ba_db_num_rows ($result) > 1)
		LogWarning ("Multiple rows in characters table with player ID $PLAYER_ID");
	//Build up UPDATE query
	$sql = "UPDATE {$db_prefix}characters SET chName = '" . ba_db_real_escape_string ($link, $_POST ['txtCharName']) . "', " .
		"chPreferredName = '" . ba_db_real_escape_string ($link, $_POST ['txtPreferredName']) . "', " .
		"chRace = '" . ba_db_real_escape_string ($link, $_POST ['txtRace']) . "', " .
		"chCulture = '" . ba_db_real_escape_string ($link, $_POST ['txtCulture']) . "', " .
		"chReason = '" . ba_db_real_escape_string ($link, $_POST ['txtReason']) . "' " .
		"WHERE chPlayerID = $PLAYER_ID";
	//Run query
	if (! ba_db_query ($link, $sql)) {
		$sWarn = "There was a problem updating your IC details";
		LogError ("Error updating character details. Player ID: $PLAYER_ID");
	}
	
	//Do not redirect if there are any warnings (required fields not filled in, etc)
	if ($sWarn == '') {
		//Get user's e-mail address
		$result = ba_db_query ($link, "SELECT plFirstName, plSurname, plEmail FROM {$db_prefix}players WHERE plPlayerID = $PLAYER_ID");
		$row = ba_db_fetch_assoc ($result);
		$email = $row ['plEmail'];
		//Send e-mail
		$sBody = "Your IC details have been entered at " . SYSTEM_NAME . ". " .
			"You need to confirm these details before you can finalise your booking.\n" .
			"Player ID: " . PID_PREFIX . sprintf ('%03s', $PLAYER_ID) . "\n" .
			"OOC Name: " . $row ['plFirstName'] . " " . $row ['plSurname'] . "\n\n" . fnSystemURL ();
		mail ($email, SYSTEM_NAME . ' - IC details', $sBody,
			"From:" . SYSTEM_NAME . " <" . EVENT_CONTACT_MAIL . ">");
		//Make up URL & redirect
		$sURL = fnSystemURL () . 'start.php?green=' . urlencode ('Your IC details have been updated');
		header ("Location: $sURL");
	}
}

include ('inc/inc_head_html.php');

//Get existing details if there are any
$sql = "SELECT * FROM {$db_prefix}characters WHERE chPlayerID = $PLAYER_ID";
$result = ba_db_query ($link, $sql);
$row = ba_db_fetch_assoc ($result);
?>

<h1><?php echo TITLE?> - IC Details</h1>

<?php
if ($sWarn != '')
	echo "<p class = 'warn'>" . $sWarn . "</p>";
?>

<p>
<i>Note: all information apart from Character Name is voluntary (and primarily to determine why your character is attending the event).
</p>

<p>
<form action = "ic_form.php" method = "post" accept-charset="iso-8859-1">

<table><tr>
<td>Character Name:</td>
<td><input type = "text" name = "txtCharName" class = "required" value = "<?php echo htmlentities (stripslashes ($row ['chName']))?>"></td>
</tr><tr>
<td>Preferred Character Name:</td>
<td><input type = "text" name = "txtPreferredName" class = 'text' value = "<?php echo htmlentities (stripslashes ($row ['chPreferredName']))?>"><?php HelpLink ('help_preferred_name.php'); ?></td>
</tr><tr>
<td>Race:</td>
<td><input type = "text" name = "txtRace" class = "text" value = "<?php echo htmlentities (stripslashes ($row ['chRace']))?>"></td>
</tr><tr>
<td>Culture:</td>
<td><input type = "text" name = "txtCulture" class = "text" value = "<?php echo htmlentities (stripslashes ($row ['chCulture']))?>"></td>
</tr>
<tr><td colspan = '2'>&nbsp;</td></tr>
<tr><td colspan = '2'><?php echo IC_NOTES_TEXT ?><br>
<textarea rows = "4" cols = "60" name = "txtReason"><?php echo htmlentities (stripslashes ($row ['chReason']))?></textarea>
</td></tr>
<tr><td colspan = '2'>&nbsp;</td></tr>
<tr><td class = 'mid'><input type = 'submit' value = 'Submit' name = 'btnSubmit'></td>
<td class = 'mid'>
<input type = 'reset' value = 'Reset'>
</td></tr>
</table>

</form>

<?php
include ('inc/inc_foot.php');
?>
